package com.alibaba.algo.apiimpl.app;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercomponent.service.app.FunctionInfoService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.CreateFunctionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.DeleteFunctionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.UpdateFunctionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.app.FunctionInfoResponseVO;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.domain.app.AppFunctionDomain;
import com.alibaba.algo.domain.app.FunctionInfoDomain;
import com.alibaba.algo.entity.app.AppFunction;
import com.alibaba.algo.entity.app.FunctionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 类FunctionInfoServiceImpl.java的实现描述：功能 serviceimpl
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Slf4j
@RestController
public class FunctionInfoServiceImpl implements FunctionInfoService {

    @Autowired
    private FunctionInfoDomain functionInfoDomain;

    @Autowired
    private AppFunctionDomain appFunctionDomain;

    @Override
    public void insert(CreateFunctionRequestVO vo) {
        FunctionInfo functionInfo = new FunctionInfo();
        BeanUtils.copyProperties(vo, functionInfo);
        functionInfoDomain.insert(functionInfo);
    }

    @Override
    public void update(UpdateFunctionRequestVO vo) {
        FunctionInfo functionInfo = new FunctionInfo();
        BeanUtils.copyProperties(vo, functionInfo);
        functionInfoDomain.updateById(functionInfo);
    }

    @Override
    public void delete(DeleteFunctionRequestVO vo) {
        Long functionId = vo.getFunctionId();
        FunctionInfo functionInfo = new FunctionInfo();
        functionInfo.setId(functionId);
        functionInfo.setIsDeleted(YesOrNo.Y.name());
        functionInfoDomain.updateById(functionInfo);

        AppFunction appFunctionCon = new AppFunction();
        appFunctionCon.setFunctionId(functionId);
        List<AppFunction> appFunctionList = appFunctionDomain.listByPara(appFunctionCon, new LinkedHashMap<>());
        for (AppFunction appFunction : appFunctionList) {
            appFunction.setIsDeleted(YesOrNo.Y.name());
            appFunctionDomain.updateById(appFunction);
        }
    }

    /**
     * 查询全量功能列表
     *
     * @return 全量功能列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult selectList() {
        FunctionInfo functionInfo = new FunctionInfo();
        List<FunctionInfo> functionInfoList = functionInfoDomain.listByPara(functionInfo, null);
        List<FunctionInfoResponseVO> resList = new ArrayList<>();
        for (FunctionInfo fi : functionInfoList) {
            FunctionInfoResponseVO resVO = new FunctionInfoResponseVO();
            BeanUtils.copyProperties(fi, resVO);
            resList.add(resVO);
        }
        return BaseResult.buildSuccess(resList);
    }

}
